package com.dangdi.show.dao;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.dysmsapi.transform.v20170525.SendSmsResponseUnmarshaller;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;


public class SmsDemo {
    public static final String MSG_TO_MANAGER_NEW_ORDER = "SMS_151546687";
    public static final String MSG_TO_USER_NEW_ORDER = "SMS_152287478";
    public static final String MSG_TO_MANAGER_ORDER_REFOND = "SMS_152282585";
    public static final String MSG_TO_USER_ORDER_REFOND = "SMS_152287497";
    public static final String MSG_TO_ACTOR_ORDER_INFO = "SMS_152282552";
    public static final String MSG_TO_USER_ORDER_OK = "SMS_152514338";
    public static final String MSG_TO_USER_MESSAGE_CODE = "SMS_155862228";
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    static final String accessKeyId = "LTAIOwPnK0V60aPj";
    static final String accessKeySecret = "usf0s673XgiOOw143Upc3tSoVvUi1E";

    public static SendSmsResponse sendSms(String phone, Map msg_content, String TemplateCode) throws ClientException, InterruptedException {

        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone);
        request.setSignName("点哏儿");
        request.setTemplateCode(TemplateCode);
        if (TemplateCode.equals("SMS_151546687")) {
            request.setTemplateParam("{\"namaner_name\":'" + msg_content.get("manager_name").toString() + "'}");
        } else if (TemplateCode.equals("SMS_152287478")) {
//            System.out.println("请致电:"+msg_content.get("manager_tel").toString());
            request.setTemplateParam("{\"manager_tel\":'" + msg_content.get("manager_tel").toString() + "'}");
        } else if (TemplateCode.equals("SMS_152282585")) {
            request.setTemplateParam("{\"namaner_name\":'" + msg_content.get("manager_name").toString() + "'}");
        } else if (TemplateCode.equals("SMS_152287497")) {
            request.setTemplateParam("{\"order_number\":'" + msg_content.get("order_number").toString() + "'}");
        } else if (TemplateCode.equals("SMS_152282552")) {
            request.setTemplateParam("{\"actor_name\":'" + msg_content.get("actor_name").toString() + "'" + ",\"show_time\":'" + msg_content.get("show_time").toString() + "',\"user_address\":'" + msg_content.get("user_address").toString() + "',\"order_feeScale_list\":'" + msg_content.get("order_feeScale_list").toString() + "',\"manager_tel\":'" + msg_content.get("manager_tel").toString() + "'}");
        } else if (TemplateCode.equals("SMS_152514338")) {
            request.setTemplateParam("{\"order_number\":'" + msg_content.get("order_number").toString() + "',\"manager_tel\":'" + msg_content.get("manager_tel").toString() + "'}");
        } else if (TemplateCode.equals("SMS_155862228")) {
            request.setTemplateParam("{\"code\":'" + msg_content.get("code").toString() + "'}");
        }



        request.setOutId("yourOutId");

        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
//        Thread.sleep(3000L);
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(sendSmsResponse.getBizId());
            System.out.println("短信明细查询接口返回数据----------------");
            System.out.println("Code=" + querySendDetailsResponse.getCode());
            System.out.println("Message=" + querySendDetailsResponse.getMessage());
            int i = 0;
            for (QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs()) {
                System.out.println("SmsSendDetailDTO[" + i + "]:");
                System.out.println("Content=" + smsSendDetailDTO.getContent());
                System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());
                System.out.println("OutId=" + smsSendDetailDTO.getOutId());
                System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
                System.out.println("ReceiveDate=" + smsSendDetailDTO.getReceiveDate());
                System.out.println("SendDate=" + smsSendDetailDTO.getSendDate());
                System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());
                System.out.println("Template=" + smsSendDetailDTO.getTemplateCode());
            }
            System.out.println("TotalCount=" + querySendDetailsResponse.getTotalCount());
            System.out.println("RequestId=" + querySendDetailsResponse.getRequestId());
        }
        return sendSmsResponse;
    }


    public static QuerySendDetailsResponse querySendDetails(String bizId) throws ClientException {

        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        QuerySendDetailsRequest request = new QuerySendDetailsRequest();
        request.setPhoneNumber("17602258294");
        request.setBizId(bizId);
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
        request.setSendDate(ft.format(new Date()));
        request.setPageSize(10L);
        request.setCurrentPage(1L);
        QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);
        return querySendDetailsResponse;
    }

    public static void main(String[] args) throws ClientException, InterruptedException {
        //发短信
//        SendSmsResponse response = sendSms("17602258294","阿力",MSG_TO_MANAGER_NEW_ORDER);
//        System.out.println("短信接口返回的数据----------------");
//        System.out.println("Code=" + response.getCode());
//        System.out.println("Message=" + response.getMessage());
//        System.out.println("RequestId=" + response.getRequestId());
//        System.out.println("BizId=" + response.getBizId());
//
//        Thread.sleep(3000L);
//
//        //查明细
//        if(response.getCode() != null && response.getCode().equals("OK")) {
//            QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(response.getBizId());
//            System.out.println("短信明细查询接口返回数据----------------");
//            System.out.println("Code=" + querySendDetailsResponse.getCode());
//            System.out.println("Message=" + querySendDetailsResponse.getMessage());
//            int i = 0;
//            for(QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs())
//            {
//                System.out.println("SmsSendDetailDTO["+i+"]:");
//                System.out.println("Content=" + smsSendDetailDTO.getContent());
//                System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());
//                System.out.println("OutId=" + smsSendDetailDTO.getOutId());
//                System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
//                System.out.println("ReceiveDate=" + smsSendDetailDTO.getReceiveDate());
//                System.out.println("SendDate=" + smsSendDetailDTO.getSendDate());
//                System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());
//                System.out.println("Template=" + smsSendDetailDTO.getTemplateCode());
//            }
//            System.out.println("TotalCount=" + querySendDetailsResponse.getTotalCount());
//            System.out.println("RequestId=" + querySendDetailsResponse.getRequestId());
//        }

    }
}
